<template>
    <div class="nav-aside">
        <el-menu :default-active="onRoutes" class="el-menu-vertical" unique-opened router>
            <el-submenu v-for="item in userMenusTree" :index="item.icon" :key="item.icon">
                <template slot="title">
                    <i class="icon" :class="['i-'+item.icon]"></i>
                    <span>{{item.name}}</span>
                    <span class="count" v-if="item.count">{{item.count}}</span>
                </template>
                <template v-for="json in item.children">
                    <el-menu-item :index="json.router">
                        <span>{{json.name}}</span>
                        <span class="count" v-if="json.count">{{json.count}}</span>
                    </el-menu-item>
                </template>
            </el-submenu>
        </el-menu>
    </div>
</template>

<script>
    import '@/assets/less/modules/asideMenu.less'
    export default {
        name: 'asideMenu',
        data() {
            return {}
        },
        computed: {
            onRoutes() {
                return this.$route.path
            },
            userMenusTree() {
                let _userMenusTree = []
                let _unProcessCounts = this.$store.getters.getUnProcess
                if (this.$store.getters.userMenus && this.$store.getters.userMenus.length) {
                    let _parentIds = []
                    this.$store.getters.userMenus.forEach((item, index) => {
                        if (item.parentId == 0) {
                            _parentIds.push(item.id)
                            let _obj = {}
                            _obj.name = item.name
                            _obj.icon = item.icon
                            if (_unProcessCounts != null) {
                                switch (item.icon) {
                                    case 'case':
                                        _obj.count = _unProcessCounts.allCaseCount
                                        break
                                    case 'clue':
                                        _obj.count = _unProcessCounts.clueCount
                                        break
                                    case 'workbench':
                                        _obj.count = _unProcessCounts.myAllCaseCount
                                        break
                                    case 'approval':
                                        _obj.count = _unProcessCounts.approver
                                        break
                                    default:
                                        break
                                }
                            }
                            _userMenusTree.push(_obj)
                        }
                    })
                    _parentIds.forEach((item, index) => {
                        _userMenusTree[index].children = []
                        this.$store.getters.userMenus.forEach(d => {
                            if (d.parentId == item) {
                                let _obj = {}
                                _obj.name = d.name
                                _obj.router = d.router
                                if (_unProcessCounts != null) {
                                    switch (d.router) {
                                        case '/case/violate':
                                            _obj.count = _unProcessCounts.allCaseCount
                                            break
                                        case '/clue/list':
                                            _obj.count = _unProcessCounts.clueCount
                                            break
                                        case '/workbench/case':
                                            _obj.count = _unProcessCounts.myAllCaseCount
                                            break
                                        case '/approval/proposal':
                                            _obj.count = _unProcessCounts.queryClueCount
                                            break
                                        case '/approval/close':
                                            _obj.count = _unProcessCounts.closeingCount
                                            break
                                        case '/approval/change':
                                            _obj.count = _unProcessCounts.switchingCount
                                            break
                                        case '/approval/pause':
                                            _obj.count = _unProcessCounts.suspendingCount
                                            break
                                        default:
                                            break
                                    }
                                }
                                _userMenusTree[index].children.push(_obj)
                            }
                        })
                    })
                }
                console.log(_userMenusTree)
                return _userMenusTree
            }
        },
        mounted() {
            if (this.$store.getters.userInfo.token) {
                if (this.$store.getters.userMenus && this.$store.getters.userMenus.length == 0) {
                    this.$store.dispatch('userMenus')
                }
                if (this.$store.getters.getUnProcess == null) {
                    this.$store.dispatch('getUnProcess')
                }
            }
        },
        methods: {}
    }
</script>